package posts

import (
	"center-data-platform/app/content/api/internal/svc"
	"center-data-platform/app/content/api/internal/types"
	"center-data-platform/app/content/rpc/content"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
)

type PostListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPostListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PostListLogic {
	return &PostListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PostListLogic) PostList(req *types.PostListReq) (resp []types.PostListResp, err error) {
	res, err := l.svcCtx.ContentRpc.List(l.ctx, &content.PostListReq{
		Ids:     req.Ids,
		UserIds: req.UserIds,
		Title:   req.Title,
		Tags:    req.Tags,
		//Status:     req.Status,
		CreateTime: req.CreateTime,
		Page:       req.Page,
		PageSize:   req.PageSize,
	})

	if err != nil {
		return nil, err
	}
	for _, v := range res.Data {
		resp = append(resp, types.PostListResp{
			Id:          v.Id,
			Title:       v.Title,
			Type:        int8(v.Type),
			Status:      int8(v.Status),
			UserId:      v.UserId,
			UserName:    v.UserName,
			CreatedAt:   v.CreatedAt,
			UpdatedAt:   v.UpdatedAt,
			Tags:        v.Tags,
			Description: v.Description,
			Cover:       v.Cover,
			Content:     v.Content,
		})
	}
	return resp, nil
}
